var SingletonCounter = (function() {

	// number times instantiated
	var instances = 0

	return function() {
		// unique instance number
		var _instance = ++instances;
	
		// get object instance number
		this.getInstanceNum = function() {
			return _instance;
		};
	};

})();

FileBrowser = function(_root_dir, _connector, _file_select_listener, _dir_select_listener, _okListener, _closeListener) {
	var counter = new SingletonCounter();
	var root_dir = _root_dir != null ? _root_dir : 'S:/Scanned Documents/Donor Awards';
	var connector = _connector;
	var file_select_listener = _file_select_listener;
	var dir_select_listener = _dir_select_listener;
	var okListener = _okListener;
	var closeListener = _closeListener;
	var title = 'Select file';
	var ID_PREFIX = 'file-browser-dialog-' + counter.getInstanceNum() + '-';
	var ID_DIALOG_CONTAINER = ID_PREFIX + 'container';
	var ID_FILE_TREE_CONTAINER = ID_PREFIX + 'file-tree';
	var BTN_CHANGE_ROOT = ID_PREFIX + 'change-root';
	var TXT_ROOT_DIR = ID_PREFIX + 'root-dir';
	var viewer = null;
	
	this.setTitle = function(_title) {
		title = _title;
	}
	
	this.open = function(newPath) {
		if(viewer == null || !$('#' + ID_DIALOG_CONTAINER).length) {
			viewer = createUi();
		}
		
		viewer.dialog("option", "title", title);
		if(newPath != undefined && root_dir != newPath) {
			$('#' + TXT_ROOT_DIR).val(newPath);
			$('#' + BTN_CHANGE_ROOT).click();
		}
		viewer.dialog('open');
	};
	
	this.close = function() {
		viewer.dialog('close');
	}
	
	function createUi() {
		var ui = $('<div id="' + ID_DIALOG_CONTAINER + '"></div>').append(
				getDialogBody());
		ui.dialog({
			modal: true,
			autoOpen : false,
			height: 560,
			width: 500,
			close: function(event, ui) {
				$(this).dialog('destroy');
			}
		});
		ui.dialog('option', 'buttons', {
			'OK' : function() {
				$(this).dialog('close');
				$('#' + ID_DIALOG_CONTAINER).remove();
				if(okListener) {
					okListener();
				}
			},
			'Close' : function() {
				$(this).dialog('close');
				$('#' + ID_DIALOG_CONTAINER).remove();
				if(closeListener) {
					closeListener();
				}
			}
			/*
			 ,
			'Cancel' : function() {
				if(file_select_listener) {
					file_select_listener('');
				}
				if(dir_select_listener) {
					dir_select_listener('');
				}
				$(this).dialog('close');
				$('#' + ID_DIALOG_CONTAINER).remove();
			}
			*/
		});
		
		$('#' + ID_FILE_TREE_CONTAINER).fileTree(
			{ root: root_dir, script: connector }, file_select_listener, dir_select_listener
		);

		$('#' + BTN_CHANGE_ROOT).click(function() {
			var new_dir = $('#' + TXT_ROOT_DIR).val();
			$('#' + ID_FILE_TREE_CONTAINER).fileTree(
				{ root: new_dir, script: connector }, file_select_listener, dir_select_listener
			);			
		});
		
		return ui;
	}
	
	function getDialogBody() {
		var htmlString = '';
		htmlString += '<div id="' + ID_FILE_TREE_CONTAINER + '" style="overflow: auto; height: 400px;">&nbsp;</div><br />';
		htmlString += '<div>Root directory: <input id="' + TXT_ROOT_DIR + '" type="text" value="' + root_dir + '" /><input id="' + BTN_CHANGE_ROOT + '" type="button" value="Change"/></div>';
		return htmlString;
	}
};